#ifndef TERMINAL_H
#define TERMINAL_H

#include <QMouseEvent>
#include <QShowEvent>
#include <QPaintEvent>
#include <QBrush>
#include <QHash>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QSpacerItem>
#include <QGridLayout>
#include <QLabel>
#include <QDragMoveEvent>
#include <QPalette>
#include <QTextEdit>
#include <QPropertyAnimation>
#include <QAnimationGroup>
#include <QEventLoop>
#include <QDesktopWidget>
#include <QApplication>
#include <QPainter>

class EffectAnimation;
enum Direction { UP=0, DOWN=1, LEFT, RIGHT, LEFTTOP, LEFTBOTTOM, RIGHTBOTTOM, RIGHTTOP, NONE };
enum BUTTON {MIN,MAX,CLOSE};

class TitleWidget;
class CenterWidget;
class EffectAnimation;

class Widget : public QWidget
{
    Q_OBJECT
public:

    Widget(QWidget* parent=nullptr);

    /**
     * @brief getBorderWidth        边框宽度
     */
    unsigned getBorderWidth();
    void setBorderWidth(const unsigned &value);

    /**
     * @brief getTitleHeight        标题栏高度
     */
    unsigned getTitleHeight();
    void setTitleHeight(const unsigned &value);

    /**
     * @brief getStatusHeight       状态栏高度
     */
    unsigned getStatusHeight();
    void setStatusHeight(const unsigned &value);

    /**
     * @brief hasBorder     有边框
     * @return
     */
    bool isHasBorder() ;
    void setHasBorder(bool value);

    /**
     * @brief hasTitleBar       有标题栏
     * @return
     */
    bool isHasTitleBar();
    void setHasTitleBar(bool value);

    /**
     * @brief setTitleColor 设置标题栏颜色
     * @param color
     */
    void setTitleColor(const QColor& color);

    /**
     * @brief getLayout     获取layout
     * @return
     */
    QVBoxLayout* getLayout();

    /**
     * @brief getPen            画笔设置
     * @return
     */
    QPen getBorderPen();
    void setBorderColor(const QColor &value);

    /**
     * @brief setButtonShow    设置关闭按钮是否显示
     * @param show
     */
    void setButtonShow(const BUTTON& button, bool show);

    /**
     * @brief getButton     获取自带button
     * @param button
     * @return
     */
    QPushButton* getButton(const BUTTON& button);

    /**
     * @brief setButtonStyle        设置Button风格
     * @param button
     * @param style
     */
    void setButtonStyle(const BUTTON& button, const QString& style);

    /**
     * @brief getCenterWidget       获取中央窗口
     * @return
     */
    QWidget *getCentreWidget();
    void setCentreWidget(QWidget *value);

    /**
     * @brief getTitle              获取标题
     * @return
     */
    QString getTitle();
    void setTitle(const QString& title);

    /**
     * @brief getIcon               获取图标
     * @return
     */
    QString getIcon();
    void setIcon(const QString& fileName);

    /**
     * @brief getTitleLable  操作标题
     * @return
     */
    QLabel* getTitleLable();
    QLabel* getIconLable();

    /**
     * @brief setBackgroundColor        设置背景颜色
     * @param color
     */
    void setBackgroundColor(const QColor& color);
    QColor getBackGroundColor() const;

    /**
     * @brief isAreaDrag                区域可拖动
     * @return
     */
    bool isAreaDrag();
    void setAreaDrag(bool value);

    /**
     * @brief getTitleWidget            获取标题窗口
     */
    QWidget *getTitleWidget();


    /**
     * @brief getTitleTextColor     标题颜色
     * @return
     */
    QColor getTitleTextColor();
    void setTitleTextColor(const QColor &value);

    /**
     * @brief addWidget     添加窗口
     * @param x
     * @param y
     * @param widget
     */
    void addWidget(int x, int y, QWidget *widget);

    void emitSizeChangeSignal();

protected:
    void mousePressEvent(QMouseEvent* e);
    void mouseMoveEvent(QMouseEvent* e);
    void mouseReleaseEvent(QMouseEvent* e);
    void paintEvent(QPaintEvent* e);
    void leaveEvent(QEvent*);
    void mouseDoubleClickEvent(QMouseEvent*e);
    virtual void drawEverything(QPainter *);

signals:
    void sizeChanged(QSize);

private:
    /**
     * @brief paintFrame        画边框
     */
    void paintFrame();

    /**
     * @brief init      初始化
     */
    void _initialze();
    QVBoxLayout* layout;
    QGridLayout* centerLayout;
    TitleWidget * titleWidget;
    QWidget* widget;
    int borderWidth;   //边框宽度
    int titleHeight;   //标题栏高度

    bool hasBorder;     //有边框
    bool hasTitleBar;   //有标题栏
    bool hasStatusBar;  //有状态栏

    QPainter * painter; //画家
    QPen borderPen;     //画笔
    QPen textPen;
    QWidget * centerWidget;
    QColor backGroundColor;
    QColor titleTextColor;      //标题颜色

    friend class TitleWidget;
    bool isLeftPressDown;  // 判断左键是否按下
    QPoint dragPosition;   // 窗口移动拖动时需要记住的点
    Direction dir;        // 窗口大小改变时，记录改变方向
    bool titleClicked;
    QString strBackGroundColor;
    bool areaDrag;

    /**
     * @brief region            获取区域
     * @param cursorGlobalPoint
     */
    void region(const QPoint &cursorGlobalPoint);

};


class TitleWidget : public QWidget
{
public:
    TitleWidget(int height, QColor titleColor, Widget *parent=nullptr);
    QPushButton* minButton, *maxButton, *closeButton;     //最小化,最大。。。
    QColor getTitleColor();
    void setTitleColor(const QColor &value);

private:
    void init();
    friend class Widget;
    QHBoxLayout* btnLayout,*btnLayoutH;

    void paintEvent(QPaintEvent *event);
    QPainter*painter;
    QColor titleColor;

    int height;
    Widget* parent;
    QLabel* title;
    QLabel* icon;
    QString strTitle;
    QString strIcon;

    void setHeight(int height);
    void setTitle(const QString& title);
    void setIcon(const QString& fileName);
    QString getTitle();
    QString getIcon();
    QLabel* getTitleLabel();
    QLabel* getIconLabel();

};


#endif // TERMINAL_H
